package com.qf.cx.simplenms.webservice.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.qf.cx.simplenms.persist.entity.TagMemoryEntity;
import com.qf.cx.simplenms.persist.service.api.TagMemoryService;
import com.qf.cx.simplenms.webservice.model.TagMemory;

@RestController
@RequestMapping(path = "/tagmemories")
public class TagMemoryController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private TagMemoryService tagMemoryService;

	@RequestMapping(method = RequestMethod.GET)
	public List<TagMemory> getTagMemories() {
		List<TagMemoryEntity> tms = tagMemoryService.getTagMemorys();
		List<TagMemory> list = new ArrayList<>();
		tms.forEach(t -> list.add(TagMemory.from(t)));
		return list;
	}

	@RequestMapping(method = RequestMethod.POST)
	public void addTagMemories(@RequestBody List<TagMemory> tms) {
		logger.debug("add tag memories {}", tms);
		List<TagMemoryEntity> tagMemoryEntities = new ArrayList<>(tms.size());
		tms.forEach(t -> tagMemoryEntities.add(t.toTagMemoryEntity()));
		tagMemoryService.addTagMemoryies(tagMemoryEntities);
	}

	@RequestMapping(value = "/{name}", method = RequestMethod.PUT)
	public void updateTagMemory(@PathVariable("name") String name, @RequestBody TagMemory tagMemory) {
		logger.debug("update tag memory {}", tagMemory);
		TagMemoryEntity tagMemoryEntity = tagMemory.toTagMemoryEntity();
		tagMemoryService.updateTagMemory(tagMemoryEntity);
	}

	@RequestMapping(value = "/{name}", method = RequestMethod.DELETE)
	public void deleteTagMemory(@PathVariable("name") String name) {
		logger.debug("delete tag memory {}", name);
		tagMemoryService.deleteTagMemory(name);
	}

}